//
//  DDRTGroupItemManager.m
//  DDRichTextDemo
//
//  Created by DaviD on 2021/9/21.
//

#import "DDRTGroupItemManager.h"
#import "DDRTHeader.h"

@interface DDRTGroupItemManager ()

@property(nonatomic,copy)NSMutableAttributedString *allGroupMemberStr;


@end

@implementation DDRTGroupItemManager

static DDRTGroupItemManager *_groupItemManager = nil;

-(instancetype)init {
    if (self = [super init]) {
        
    }
    return self;
}

+(instancetype)shareGroupItemManagerInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        _groupItemManager = [[DDRTGroupItemManager alloc] init];
    });
    return _groupItemManager;
}

// 选中的状态
-(void)selectedItemState:(DDRTGroupMemberModel *)model {
    model.isClick = YES;
    
}
// 未选中的状态
-(void)cancleItemState:(DDRTGroupMemberModel *)model {
    model.isClick = NO;
}


-(void)addGroupItem:(DDRTGroupMemberModel *)model {
    
    [self.groupMemberArr addObject:model];
}

-(void)deleteGroupItem:(DDRTGroupMemberModel *)model {
    model.isSelected = NO;
    [self.groupMemberArr removeObject:model];
}

// 返回表示有没有可删除的内容，没有的话，把最后一个选中,并返回NO，默认返回YES
-(BOOL)deleteSelectedGroupArr {
    
    if (self.groupMemberArr != nil && self.groupMemberArr.count != 0) {
        NSMutableArray *deleteArr = [NSMutableArray array];
        for (DDRTGroupMemberModel *model in self.groupMemberArr) {
            if (model.isClick == YES) {
                model.isSelected = NO;
                [deleteArr addObject:model];
            }
           
        }
        if (deleteArr.count) {
            for (DDRTGroupMemberModel *friendsModel in deleteArr) {
                [self deleteGroupItem:friendsModel];
            }
        }else {
            DDRTGroupMemberModel *lastModel = [self.groupMemberArr lastObject];
            lastModel.isClick = YES;
            return NO;
        }
        
    }
    else {
        return NO;
    }
    return YES;
    
}

// 根据传入的下标，获得到对应数组的位置
-(NSInteger)indexOfGroupLoctaion:(NSInteger)location {
    NSInteger nowIndex = 0;
    if (self.groupMemberArr && self.groupMemberArr.count) {
        for (int index = 0; index < self.groupMemberArr.count; index ++ ) {
            DDRTGroupMemberModel *model = self.groupMemberArr[index];
            if (model.nameLocation >= location) {
                nowIndex = index;
                break;
            }
        }
    }
    return nowIndex;
    
}

// 获取被处理过的富文本字符串
-(NSMutableAttributedString *)getAttributeAllMemberStr {
    
    // 清空字符串
    [self.allGroupMemberStr replaceCharactersInRange:NSMakeRange(0, self.allGroupMemberStr.mutableString.length) withString:@""];
    
    if (self.groupMemberArr && self.groupMemberArr.count) {
        NSInteger totalLocation = 0;
        for (int index = 0; index < self.groupMemberArr.count; index ++) {
            DDRTGroupMemberModel *memberModel = self.groupMemberArr[index];
            memberModel.nameLocation = totalLocation;
            NSString *nameStr;
            NSMutableAttributedString *attrStr;
            BOOL isLast = NO;
            if (index == self.groupMemberArr.count - 1) {// 最后一位的话，不拼接对应的字符串
                nameStr = memberModel.personName;
                isLast = YES;
            }
            else {
                
                nameStr = [NSString stringWithFormat:@"%@%@",memberModel.personName,kAppendStrSign];
                isLast = NO;
            }
            totalLocation = totalLocation + nameStr.length;
            
            
            if (memberModel.isClick == YES) {
                attrStr = [self getSubStringClick:nameStr andFont:kRichTextViewFont andIsLast:isLast];
            }
            else {
                attrStr = [self getSubStringNormal:nameStr andFont:kRichTextViewFont andIsLast:isLast];
            }
            
            [self.allGroupMemberStr appendAttributedString:attrStr];

        }
    }
    return self.allGroupMemberStr;
}

// 第一次进来时，更新所有数据源的点击状态为普通状态
-(void)updateAllItemStateToNomal {
    if (self.groupMemberArr.count) {
        for (int index = 0; index < self.groupMemberArr.count; index ++ ) {
            DDRTGroupMemberModel *memberModel = self.groupMemberArr[index];
            memberModel.isClick = NO;
        }
    }
    
}


// 传递一串字符串，获取最开始的Normal状态
-(NSMutableAttributedString *)getSubStringNormal:(NSString *)str andFont:(UIFont *)font andIsLast:(BOOL)isLast {
    
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:str];
    NSRange range = NSMakeRange(0, attrStr.length);
    [attrStr addAttribute:NSFontAttributeName value:font range:range];
    if (isLast == YES) {
        [attrStr addAttribute:NSForegroundColorAttributeName value:kThemeOrangeColor range:NSMakeRange(0, attrStr.length)];

    }else {
        [attrStr addAttribute:NSForegroundColorAttributeName value:kThemeOrangeColor range:NSMakeRange(0, attrStr.length - 3)];

    }
    [attrStr addAttribute:NSLinkAttributeName value:str range:NSMakeRange(0, attrStr.length)];

    return attrStr;
}

// 传递一串字符串，获取点击后的状态
-(NSMutableAttributedString *)getSubStringClick:(NSString *)str andFont:(UIFont *)font andIsLast:(BOOL)isLast {
    
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:str];
    NSRange range = NSMakeRange(0, attrStr.length);
    [attrStr addAttribute:NSFontAttributeName value:font range:range];
    
    if (isLast == YES) {
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, attrStr.length)];

        [attrStr addAttribute:NSBackgroundColorAttributeName value:kThemeOrangeColor range:NSMakeRange(0, attrStr.length)];

    }else {
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, attrStr.length - 3)];
        [attrStr addAttribute:NSBackgroundColorAttributeName value:kThemeOrangeColor range:NSMakeRange(0, attrStr.length - 3)];

    }
    
    [attrStr addAttribute:NSLinkAttributeName value:str range:NSMakeRange(0, attrStr.length)];

    return attrStr;
}

#pragma mark - Setter & Getter

-(NSMutableArray *)groupMemberArr {
    if (!_groupMemberArr) {
        _groupMemberArr = [NSMutableArray array];
    }
    return _groupMemberArr;
}

-(NSMutableAttributedString *)allGroupMemberStr {
    if (!_allGroupMemberStr) {
        _allGroupMemberStr = [[NSMutableAttributedString alloc] init];
    }
    return _allGroupMemberStr;
}


@end
